package com.heartline.pojo;

import com.blade.kit.DateKit;
import lombok.AllArgsConstructor;
import lombok.Builder;

/**
 * RestResponse
 *
 * QuoteFieldNames———-输出key时是否使用双引号,默认为true
 * WriteMapNullValue——–是否输出值为null的字段,默认为false
 * WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null
 * WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null
 * WriteNullStringAsEmpty—字符类型字段如果为null,输出为"",而非null
 * WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null
 *
 * @param <T>
 * @since 2.0.2-beta
 */
@Builder
@AllArgsConstructor
public class RestResponse<T> {

    /**
     * 服务器响应数据
     */
    private T data;

    /**
     * 错误信息
     */
    @Builder.Default
    private String msg = "fail";

    /**
     * 状态码
     */
    @Builder.Default
    private int code = -1;

    /**
     * 服务器响应时间
     */
    @Builder.Default
    private long timestamp = DateKit.nowUnix();

    public RestResponse() {
        this.timestamp = DateKit.nowUnix();
    }

    public RestResponse(T data) {
        this.timestamp = DateKit.nowUnix();
        this.data = data;
    }

    public T getData() {
        return data;
    }

    public String getMsg() {
        return msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public static RestResponse ok() {
        return RestResponse.builder().code(0).msg("success").build();
    }

    public static <T> RestResponse ok(T data) {
        return RestResponse.builder().data(data).msg("success").code(0).build();
    }

    public static <T> RestResponse ok(T data, int code) {
        return RestResponse.builder().data(data).msg("success").code(code).build();
    }

    public static RestResponse fail() {
        return RestResponse.builder().build();
    }

    public static RestResponse fail(String msg) {
        return RestResponse.builder().msg(msg).build();
    }

    public static RestResponse fail(int code) {
        return RestResponse.builder().code(code).build();
    }

    public static RestResponse fail(int code, String msg) {
        return RestResponse.builder().msg(msg).code(code).build();
    }

}